﻿using FileHelpers;

namespace SecurityModule.Parsers.Format2014
{
    /// <summary>
    /// Represents a line in the IDOR tax file as of 2014 to be returned when parsing CSV files using <see cref="SecurityModule.Parsers.Format2014.TaxParser"/> which uses <see cref="FileHelperEngine{T}"/>.
    /// </summary>
    [DelimitedRecord(",")]
    internal class TaxLine
    {
        [FieldConverter(typeof(CleanStringConverter))]
        public int AccountNumber;
        [FieldConverter(typeof(CleanStringConverter))]
        public int Site;
        [FieldConverter(typeof(MoneyConverter))]
        public string Val1;
        [FieldConverter(typeof(MoneyConverter))]
        public string Val2;
        [FieldConverter(typeof(MoneyConverter))]
        public string Val3;
        [FieldConverter(typeof(MoneyConverter))]
        public string Val4;

        public class MoneyConverter : ConverterBase
        {
            public override object StringToField(string from)
            {
                if (from.StartsWith("(")) // remove paranthesis and add a negative sign
                    from = "-" + from.Substring(1, from.Length - 2);
                return from.Replace("$", "");
            }
        }

        public class CleanStringConverter : ConverterBase
        {
            public override object StringToField(string from)
            {
                var cleanString = from.Replace("\"", "").Trim();
                if (cleanString.Equals(string.Empty))
                    return -1;
                if (cleanString.ToLower().Equals("totals"))
                    return -1;
                int result;
                if (int.TryParse(cleanString, out result))
                    return result;
                return -1;
            }
        }
    }
}